package bingliandshuju;

import java.io.Serializable;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import zhzz.SingleDataofzhzz;

import com.example.animaldoctor03.AllIllness;
import com.example.animaldoctor03.JBXQ;
import com.example.animaldoctor03.R;
import com.example.animaldoctor03.YiXuanZhengZhuang;
import com.example.animaldoctor03.ZhengDuanFF;
import com.example.animaldoctor03.ZhengZhuangFZ;

import commonClass.ADAQueryClinicalSymptomsKeyByDiseaseIDAction;
import commonClass.FunctionDispatcher;
import commonClass.RemoteActionsName;
import commonClass.FunctionDispatcher.IRequestCompeletListener;
import Modle.disease.ClinicalSymptomsKey;
import Modle.disease.Symptoms;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Notification.Builder;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

public class BingLi5 extends Activity {

	private String nameofBL5 = "";
	private String GLofBL5;
	private TextView textofBL5 = null;
	private ListView listviewofbingli5 = null;
	private Button buttonofbingli5toJBXQ = null,
			buttonofbingli5forallchoice = null;
	private Button buttonofbingli5toagain = null, buttonofBL5ofYes = null,
			buttonofBL5F = null;

	private Symptoms symptomsofBL5getinformation = null;
	private List<Object> listofBL5getinformation = new ArrayList<Object>();
	private String animalIDofBL5 = "";
	private ArrayList<Object> resultListofBL5 = null;

	private void initElemofBL5() {
		setContentView(R.layout.bingli5);
		buttonofBL5F = (Button) findViewById(R.id.buttonofBL5F);
		textofBL5 = (TextView) findViewById(R.id.textviewofbingli5);
		listviewofbingli5 = (ListView) findViewById(R.id.listofbingli5);
		buttonofbingli5forallchoice = (Button) findViewById(R.id.buttonofbingli54);
		buttonofbingli5toJBXQ = (Button) findViewById(R.id.buttonofbingli51);
		buttonofbingli5toagain = (Button) findViewById(R.id.buttonofbingli52);

		buttonofbingli5toJBXQ.setOnClickListener(new listenetofbingli5toJBXQ());
		buttonofbingli5toagain
				.setOnClickListener(new listenerofbingli5toagain());
		buttonofBL5F.setOnClickListener(new listenerofBL5F());
	}

	private void getInformationofBL5() {

		SingleDataofAll singleDataofBL5 = SingleDataofAll.getInstance();
		symptomsofBL5getinformation = singleDataofBL5.getObject();
		listofBL5getinformation = singleDataofBL5.getListinformation();
	}

	private void getInformationfromupofBL5() {
		Intent intentofBL5 = getIntent();
		Bundle bundleofBL5 = intentofBL5.getExtras();
		ClinicalSymptomsKey CofBL5 = (ClinicalSymptomsKey) bundleofBL5
				.getSerializable("shuju");
		animalIDofBL5 = CofBL5.getDiseaseID();
		nameofBL5 = CofBL5.getDiseaseName();
		DecimalFormat formatofBL5 = new DecimalFormat("#.0000");
		GLofBL5 = formatofBL5.format(CofBL5.getDiseaseProbability());
		Message msgofBL5 = new Message();
		msgofBL5.what = 1;
		handleofBL5.sendMessage(msgofBL5);
	}

	private ArrayList<Object> assemblyofBL5() {

		getInformationfromupofBL5();
		ArrayList<Object> list = new ArrayList<Object>();

		list.add(animalIDofBL5);
		// Integer i = new Integer(10);
		// list.add(i);

		return list;
	}

	private void bindingofBL5(final ArrayList<Object> list) {
		try {
			getInformationofBL5();
			List<String> listofoldIdsofBL5 = new ArrayList<String>();
			listofoldIdsofBL5.add(symptomsofBL5getinformation.getSymptomsID());
			for (int i = 0; i < listofBL5getinformation.size(); i++) {

				ClinicalSymptomsKey ctextofBL5 = (ClinicalSymptomsKey) listofBL5getinformation
						.get(i);
				listofoldIdsofBL5.add(ctextofBL5.getClinicalSymptomsKeyID());

			}
			List<Object> listofZZhuanofBL5 = new ArrayList<Object>();

			List<Object> listofBL5QC = new ArrayList<Object>();

			for (int j = 0; j < list.size(); j++) {

				ClinicalSymptomsKey cetext1ofBL5 = (ClinicalSymptomsKey) list
						.get(j);
				String sofctext1ofBL5 = cetext1ofBL5.getClinicalSymptomsKeyID();
				for (String s : listofoldIdsofBL5) {
					if (sofctext1ofBL5.equals(s)) {

						listofBL5QC.add(cetext1ofBL5);
					}

				}
			}

			for (int i = 0; i < list.size(); i++) {

				int jishuofBL5 = 0;
				ClinicalSymptomsKey ctext2ofBL5 = (ClinicalSymptomsKey) list
						.get(i);
				String sofctext2ofBL5 = ctext2ofBL5.getClinicalSymptomsKeyID();
				for (String s : listofoldIdsofBL5) {
					if (!sofctext2ofBL5.equals(s)) {
						jishuofBL5++;

					}
					if (jishuofBL5 == listofoldIdsofBL5.size()) {
						listofZZhuanofBL5.add(ctext2ofBL5);
					}

				}
			}

			String[] resultofBL5 = new String[listofZZhuanofBL5.size()];
			// list.toArray(new String[list.size()]);
			for (int i = 0; i < listofZZhuanofBL5.size(); i++) {

				// haiyouwenti
				resultofBL5[i] = ((ClinicalSymptomsKey) listofZZhuanofBL5
						.get(i)).getClinicalSymptomsKey();
			}
			ArrayAdapter<String> arrayAdapterofBL5 = new ArrayAdapter<>(this,
					android.R.layout.simple_list_item_multiple_choice,
					resultofBL5);
			listviewofbingli5.setAdapter(arrayAdapterofBL5);
			buttonofbingli5forallchoice.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					ArrayList<Object> listofnextofBL5 = new ArrayList<Object>();

					for (long ids : listviewofbingli5.getCheckItemIds()) {

						int i = (int) ids;

						listofnextofBL5.add((ClinicalSymptomsKey) list.get(i));
					}

					// 把数据储存好
					SingleDataofAll singleDataofAllofBL5 = SingleDataofAll
							.getInstance();
					singleDataofAllofBL5.setListinformation(listofnextofBL5);
					try {
						Intent intentofbingli5toallchoice = new Intent(BingLi5.this,
								YiXuanZhengZhuang.class);
						startActivity(intentofbingli5toallchoice);
						finish();
					} catch (RuntimeException e) {
						android.app.AlertDialog.Builder builderofBL5oftoYXZZ = new AlertDialog.Builder(
								BingLi5.this);
						builderofBL5oftoYXZZ.setMessage("该方法无法使用,请重试或者使用其他方法");
						builderofBL5oftoYXZZ.create().show();
					}

				}
			});
		} catch (NullPointerException e) {
			android.app.AlertDialog.Builder builderofBL5 = new AlertDialog.Builder(
					BingLi5.this);
			builderofBL5.setMessage("无数据  , 请用其他功能");
			builderofBL5.create().show();

		}
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		initElemofBL5();
		ArrayList<Object> parasListofBL5 = assemblyofBL5();

		final ArrayList<Object> resultListTofBL5 = null;

		ADAQueryClinicalSymptomsKeyByDiseaseIDAction iActionofBL5 = new ADAQueryClinicalSymptomsKeyByDiseaseIDAction();
		FunctionDispatcher functionDispacherofBL5 = new FunctionDispatcher();

		functionDispacherofBL5.exceute(
				RemoteActionsName.QueryClinicalSymptomsKeyByDiseaseIDAction,
				parasListofBL5, iActionofBL5, new IRequestCompeletListener() {

					@Override
					public void doCompelet(ArrayList<Object> resultList) {
						// TODO Auto-generated method stub
						try {
							Log.v("ZhengZhuangFZ2",
									"post:" + resultList.toString());
							BingLi5.this.resultListofBL5 = resultList;
							bindingofBL5(resultList);
						} catch (NullPointerException e) {
							android.app.AlertDialog.Builder builderofBL5off = new AlertDialog.Builder(
									BingLi5.this);
							builderofBL5off.setMessage("无返回值, 请用其他功能");
							builderofBL5off.create().show();
						}
					}
				}, this);

	}

	class listenerofBL5F implements OnClickListener {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			try {
				SingleDataofAll sAll = SingleDataofAll.getInstance();
				sAll.removeAll();
				SingleInformationofIllnessDetail sInfor = SingleInformationofIllnessDetail
						.getInstance();
				sInfor.removeAll();
				SingleDataofzhzz sDofzh = SingleDataofzhzz.getInstance();
				sDofzh.removeAll();
				Intent intentofBL5F = new Intent(BingLi5.this,
						ZhengDuanFF.class);
				startActivity(intentofBL5F);
				finish();
			} catch (RuntimeException e) {
				android.app.AlertDialog.Builder builderofBL5ofF = new AlertDialog.Builder(
						BingLi5.this);
				builderofBL5ofF.setMessage("该方法无法使用,请重试或者使用其他方法");
				builderofBL5ofF.create().show();
			}
		}

	}

	class listenerofbingli5toagain implements OnClickListener {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			try {
				List<Object> listofagainofBL5 = new ArrayList<Object>();
				SingleDataofAll singDataofallofBL5 = SingleDataofAll
						.getInstance();
				listofagainofBL5 = singDataofallofBL5.getListinformation();
				Bundle bundleofagainofBL5 = new Bundle();
				bundleofagainofBL5.putSerializable("shuju",
						(Serializable) listofagainofBL5);
				Intent intentofbingli5toagain = new Intent(BingLi5.this,
						AllIllness.class);
				intentofbingli5toagain.putExtras(bundleofagainofBL5);
				startActivity(intentofbingli5toagain);
				finish();
			} catch (RuntimeException e) {
				android.app.AlertDialog.Builder builderofBL5oftoA = new AlertDialog.Builder(
						BingLi5.this);
				builderofBL5oftoA.setMessage("该方法无法使用,请重试或者使用其他方法");
				builderofBL5oftoA.create().show();
			}
		}

	}


	class listenetofbingli5toJBXQ implements OnClickListener {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			try {
				Bundle bundleofBL5 = new Bundle();
				bundleofBL5.putString("shuju", animalIDofBL5);
				Intent intentofbingli5toJBXQ = new Intent(BingLi5.this,
						JBXQ.class);
				intentofbingli5toJBXQ.putExtras(bundleofBL5);
				startActivity(intentofbingli5toJBXQ);
				finish();
			} catch (RuntimeException e) {
				android.app.AlertDialog.Builder builderofBL5oftoJBXQ = new AlertDialog.Builder(
						BingLi5.this);
				builderofBL5oftoJBXQ.setMessage("该方法无法使用,请重试或者使用其他方法");
				builderofBL5oftoJBXQ.create().show();
			}

		}

	}

	public Handler handleofBL5 = new Handler() {

		public void handleMessage(Message msg) {

			if (msg.what == 1) {
				textofBL5.setText("病名：" + nameofBL5 + " " + "概率：" + "0"
						+ GLofBL5);
			}
		}
	};
}
